<template>
  <div
    id="accountAdd"
    class="dialog animated"
    :class="open? 'fadeIn':'fadeOut'"
    v-show="show"
    :style="{visibility: (visible?'visible':'hidden')}"
  >
    <div class="panel animated" :class="open? 'zoomIn':''">
      <div class="title">
        {{title}}
        <div class="close" @click="cancel()"></div>
      </div>
      <div class="form">
        <el-form :model="accountData" ref="accountData" label-width="140px">
          <el-form-item label="登录账号" :rules="[
      { required: true, message: '登录账号不能为空'}]">
            <el-input
              style="width:35%"
              type="tel"
              v-model="accountData.username"
              placeholder="请输入登录系统的手机号"
            ></el-input>
          </el-form-item>
          <el-form-item label="名称" :rules="[
      { required: true, message: '名称不能为空'}]">
            <el-input
              style="width:35%"
              maxlength="10"
              clearable
              v-model="accountData.nickName"
              placeholder="请输入账号对应的名称"
            ></el-input>
          </el-form-item>
          <el-form-item
            v-if="isHasSelectOrg"
            label="所属机构"
            :rules="[
      { required: true, message: '所属机构不能为空'}]"
          >
            <el-select
              v-model="accountData.orgId"
              clearable
              multiple
              collapse-tags
              @change="orgChange"
              placeholder="选择账号所属的机构"
              style="width:35%"
            >
              <el-option
                v-for="item in orgList"
                :key="item.orgId"
                :label="item.orgName"
                :value="item.orgId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            v-if="isHasSelectCampus"
            label="所属校区"
            :rules="[
            { required: true, message: '所属校区不能为空'}]"
          >
            <el-cascader
              style="width:50%"
              placeholder="选择账号所属的校区"
              v-model="accountData.campusId"
              @change="campusChange"
              :options="orgSchoolDict"
              :props="{ multiple: true }"
              collapse-tags
              filterable
              clearable
            ></el-cascader>
          </el-form-item>
          <el-form-item v-if="isAdmin" label="角色形式" :rules="[]">
            <el-select
              v-model="roleType"
              placeholder="选择账号所属的角色形式"
              style="width:35%"
              @change="changeRoleType"
            >
              <el-option label="角色组" :value="1"></el-option>
              <el-option label="角色项" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="角色"
            v-if="roleType == 2"
            :rules="[
      { required: true, message: '角色不能为空'}]"
          >
            <el-radio-group v-model="accountData.roleId" @change="handleCheckedCitiesChange">
              <el-radio
                border
                :label="item.roleId"
                v-for="item in roleList"
                v-bind:key="item.roleId"
              >{{item.roleName}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            label="角色组"
            v-if="roleType == 1"
            :rules="[
      { required: true, message: '角色不能为空'}]"
          >
            <el-radio-group v-model="accountData.roleGroupId" @change="handleRoleGroupChange">
              <el-radio
                border
                :label="item.roleGroupId"
                v-for="item in roleAttachList"
                v-bind:key="item.roleGroupId"
              >{{item.roleGroupName}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            label="管辖范围"
            v-if="isHasSelectArea"
            :rules="[
      { required: true, message: ''}]"
          >
            <el-tag
              :key="tag.popedomCode"
              v-for="tag in userPopedomList"
              :disable-transitions="false"
              closable
              @close="handleClose(tag)"
            >{{tag.popedomName}}</el-tag>
            <el-button
              v-if="isCanAddArea"
              class="button-new-tag"
              size="small"
              @click="addArea"
            >十 {{areaText}}</el-button>
          </el-form-item>
          <!-- <el-form-item
            label="所属标签"
            v-if="selectTagList.length>0"
            :rules="[
      { required: true, message: ''}]"
          >
            <el-checkbox-group v-model="hasSelectTagList" style="float:left;">
              <el-checkbox
                v-for="item in selectTagList"
                :key="item.tagId"
                :label="item.tagId"
              >{{item.tagName}}</el-checkbox>
            </el-checkbox-group>
            
            <el-tag
              
              v-for="tag in selectTagList"
              :disable-transitions="false"
              closable
              @close="handleCloseTag(tag)"
            >{{tag.tagName}}</el-tag>
            <el-button
              v-if="isCanAddTag"
              style="float:left;margin-left:0.3rem;"
              class="button-new-tag"
              size="small"
              @click="addTags"
            >添加标签</el-button>
          </el-form-item> -->
          <el-form-item label="创建人" v-if="!isAdd">
            <span>{{accountData.createUserName}}</span>
          </el-form-item>
          <el-form-item label="创建时间" v-if="!isAdd">
            <span>{{accountData.createTime}}</span>
          </el-form-item>
        </el-form>
        <!-- <div class="line_from"></div> -->
        <div class="btn_div">
          <div class="btn_sure" @click="sure()">确 认</div>
          <div class="btn_cancel" @click="cancel()">取 消</div>
        </div>
        <!-- <div class="line_from"></div>
        <el-row class="tree">
          <span style="font-size:16px;color:#666666;">选择的角色权限列表</span>
          <el-row style="margin-top: 0.2rem;" v-if="!isAdmin">
            <el-col :span="24">
              <div class="grid-content bg-purple">菜单名称</div>
            </el-col>
          </el-row>
          <el-row style="margin-top: 0.2rem;" v-else>
            <el-col :span="16">
              <div class="grid-content bg-purple">菜单名称</div>
            </el-col>
            <el-col :span="8">
              <div class="grid-content bg-purple">类型</div>
            </el-col>
          </el-row>
          <el-tree
            ref="tree"
            class="tree"
            :data="sysRoledata"
            :props="defaultProps"
            show-checkbox
            node-key="menuId"
            :default-expanded-keys="accountData.menuIds"
            :default-checked-keys="accountData.menuIds"
            check-strictly
            :render-content="renderContent"
        ></el-tree>-->
        <!-- </el-row> -->
      </div>
    </div>
    <areaAdd ref="areaAdd" @callback="closeAdd"></areaAdd>
    <cityAdd ref="cityAdd" @callback="closeAdd"></cityAdd>
  </div>
</template>

<script>
import areaAdd from "./area_add";
import cityAdd from "./city_addd";
import { setTimeout } from "timers";
var vm;
var user;
export default {
  components: {
    areaAdd,
    cityAdd
  },
  data: function() {
    return {
      show: false,
      open: false,
      visible: false,
      isAdd: true,
      isAdmin: false,
      isHasSelectOrg: true, //是否需要选择机构
      isHasSelectCampus: true, //是否需要选择校区
      isHasSelectArea: false, //是否需要选择管辖区域
      title: "",
      roleType: 2,
      areaText: "",
      isCanAddArea: false, //是否能添加管辖区域
      isCanAddTag: false, //是否能添加标签
      accountData: {},
      sysRoledata: [],
      roleList: [],
      roleAttachList: [],
      orgList: [],
      campusList: [],
      currentRole: {},
      defaultProps: {
        children: "list",
        label: "name"
      },
      userPopedomList: [], //选中的管辖区域列表
      orgAreaList: [], //机构管辖区域列表
      orgSchoolDict: [], //机构校区列表
      tagList: [], //标签列表
      selectTagList: [], //标签列表
      hasSelectTagList: [] //已选择的标签列表
    };
  },
  methods: {
    /**选择校区回调 */
    campusChange(val) {
      
      vm.selectTagList = [];
      
      if (vm.accountData.campusId) {
        let idOfCampusAndOrg = [];
        console.log(vm.accountData.campusId);
        for (let i = 0; i < vm.accountData.campusId.length; i++) {
          idOfCampusAndOrg.push({
            orgId:vm.accountData.campusId[i][0],
            campusId:vm.accountData.campusId[i][1],
          })
          if (
            i != 0 &&
            vm.accountData.campusId[i][0] == vm.accountData.campusId[i - 1][0]
          )
            continue;
          for (let y = 0; y < vm.tagList.length; y++) {
            if (vm.accountData.campusId[i][0] == vm.tagList[y].orgId) {
              Array.prototype.push.apply(vm.selectTagList, vm.tagList[y].tags);
            }
          }
        }
        vm.api_getOtherData(idOfCampusAndOrg);
      }
      
    },
    /**添加省份、城市回调 */
    closeAdd(data) {
      console.log(data);
      vm.userPopedomList = vm.transformItem(data, true);
    },
    // handleCloseTag(item) {
    //   let index = vm.selectTagList.indexOf(item);
    //   vm.selectTagList.splice(index, 1);
    // },
    /**删除所选管辖区域 */
    handleClose(item) {
      console.log(item);
      if (item.popedomCode == null) return;
      let index = vm.userPopedomList.indexOf(item);
      vm.userPopedomList.splice(index, 1);
    },
    /**添加标签 */
    addTags() {
      if (!vm.$store.state.permit["54"]) {
        vm.$message({
          message: "您没有权限操作，请添加权限",
          type: "error"
        });
        return;
      }
      var path = vm.$store.state.permit["54"].path;
      //传递参数
      this.$router.push({
        path: path,
        query: { isShowAdd: tool.encode("1") }
      });
    },
    /**管辖区域字段转换 */
    transformItem(transformList, isFront) {
      let returnList = [];
      if (isFront) {
        for (let i = 0; i < transformList.length; i++) {
          returnList.push({
            popedomCode: transformList[i].value,
            popedomName: transformList[i].label
          });
        }
      } else {
        for (let i = 0; i < transformList.length; i++) {
          returnList.push({
            value: transformList[i].popedomCode,
            label: transformList[i].popedomName
          });
        }
      }
      return returnList;
    },
    /**添加省份/城市 */
    addArea() {
      if (vm.areaText == "添加省份") {
        vm.$refs.areaAdd.init({
          data: vm.transformItem(vm.userPopedomList, false)
        });
      } else {
        vm.$refs.cityAdd.init({
          data: vm.transformItem(vm.userPopedomList, false)
        });
      }
    },
    handleRoleGroupChange(val) {
      console.log(vm.roleAttachList);
      vm.currentRole = vm.roleAttachList.find(role => role.roleGroupId == val);
      console.log(vm.currentRole);
      vm.isHasSelectOrg = vm.currentRole.mainRoleMode <= 1;
      vm.isHasSelectCampus = vm.currentRole.mainRoleMode > 1;
      vm.accountData.menuIds = [];
      vm.sysRoledata = [];
      if (vm.currentRole.childRoleMode == null) {
        vm.isHasSelectArea = false;
      } else {
        vm.isHasSelectArea = true;
        let defaultAreaList = [];
        if (
          vm.currentRole.childRoleMode != 1 &&
          vm.accountData.orgId.length > 0
        ) {
          defaultAreaList = vm.getDefaultAreaList();
        }
        vm.getAreaInfo(vm.currentRole.childRoleMode, defaultAreaList);
      }
    },
    /**
     * 选择角色
     */
    handleCheckedCitiesChange(value) {
      // console.log(value);
      let paras = {
        roleId: value
      };
      let roleListTemp = {};
      let menuIds = [];
      // vm.selectTagList = [];
      vm.currentRole = vm.roleList.find(role => role.roleId == value);
      // vm.accountData.orgId = [];
      vm.isHasSelectOrg = vm.currentRole.roleMode <= 1;
      vm.isHasSelectCampus = vm.currentRole.roleMode > 1;
      if (vm.currentRole.childRoleMode == null) {
        vm.isHasSelectArea = false;
      } else {
        vm.isHasSelectArea = true;
        let defaultAreaList = [];
        if (
          vm.currentRole.childRoleMode != 1 &&
          vm.accountData.orgId.length > 0
        ) {
          defaultAreaList = vm.getDefaultAreaList();
        }
        vm.getAreaInfo(vm.currentRole.childRoleMode, defaultAreaList);
      }
      // vm.accountData.menuIds = [];
      // api.selectRoleMenus(paras).then(function(data) {
      //   if (data.result == "操作成功") {
      //     menuIds = data.list;
      //     vm.getMenuIds(menuIds);
      //     vm.sysRoledata = data.list;
      //     vm.getList();
      //     setTimeout(function() {
      //       vm.$refs.tree.setCheckedKeys(vm.accountData.menuIds);
      //     }, 500);
      //   }
      // });
    },
    /**改变机构 */
    orgChange(val) {
      console.log(val);
      vm.selectTagList = [];
      for (let i = 0; i < vm.accountData.orgId.length; i++) {
        for (let y = 0; y < vm.tagList.length; y++) {
          if (vm.accountData.orgId[i] == vm.tagList[y].orgId) {
            //去掉重复的标签
            var removalList = vm.tagList[y].tags.filter(newItem => {
              return !vm.selectTagList.some(
                defaultItem => defaultItem.tagId == newItem.tagId
              );
            });
            Array.prototype.push.apply(vm.selectTagList, removalList);
          }
        }
      }
      if (vm.currentRole.childRoleMode != null) {
        let defaultAreaList = [];
        if (
          vm.currentRole.childRoleMode != 1 &&
          vm.accountData.orgId.length > 0
        ) {
          let orgIdlist = [val[val.length - 1]];
          defaultAreaList = vm.getDefaultAreaList(
            vm.userPopedomList,
            orgIdlist
          );
        }
        vm.getAreaInfo(vm.currentRole.childRoleMode, defaultAreaList);
      }
    },
    /**获取默认的管辖省份/城市 */
    getDefaultAreaList(defaultAreaList = [], orgIdList = vm.accountData.orgId) {
      // let defaultAreaList = defaultAreaList;
      // orgIdList = orgIdList ? vm.accountData.orgId : orgIdList;
      console.log(orgIdList);
      for (let i = 0; i < orgIdList.length; i++) {
        for (let n = 0; n < vm.orgAreaList.length; n++) {
          if (vm.accountData.orgId[i] == vm.orgAreaList[n].value) {
            //选择的是省份
            if (vm.currentRole.childRoleMode == 2) {
              let newAreaList = vm.transformItem(
                vm.orgAreaList[n].provinceRegions,
                true
              );
              //去掉重复的省份/城市
              var removalList = newAreaList.filter(newItem => {
                return !defaultAreaList.some(
                  defaultItem => defaultItem.popedomCode == newItem.popedomCode
                );
              });
              console.log(removalList);
              Array.prototype.push.apply(defaultAreaList, removalList);
              //选择的是城市
            } else if (vm.currentRole.childRoleMode == 3) {
              let newAreaList = vm.transformItem(
                vm.orgAreaList[n].cityRegions,
                true
              );
              //去掉重复的省份/城市
              var removalList = newAreaList.filter(newItem => {
                return !defaultAreaList.some(
                  defaultItem => defaultItem.popedomCode == newItem.popedomCode
                );
              });
              console.log(removalList);
              Array.prototype.push.apply(defaultAreaList, removalList);
            }
          }
        }
      }
      console.log(defaultAreaList);
      return defaultAreaList;
    },
    changeRoleType(val) {
      if (val == 1) {
        vm.accountData.orgId = [];
        vm.accountData.campusId = [];
        vm.accountData.roleGroupId = vm.roleAttachList[0].roleGroupId;
        vm.currentRole = vm.roleAttachList[0];
        vm.isHasSelectOrg = vm.currentRole.mainRoleMode <= 1;
        vm.isHasSelectCampus = vm.currentRole.mainRoleMode > 1;
        vm.accountData.menuIds = [];
        vm.sysRoledata = [];
      } else {
        let paras = {};
        let menuIds = [];
        vm.accountData.roleId = vm.roleList[0].roleId;
        vm.currentRole = vm.roleList[0];
        paras.roleId = vm.roleList[0].roleId;
        vm.isHasSelectOrg = vm.currentRole.roleMode <= 1;
        vm.isHasSelectCampus = vm.roleList[0].roleMode > 1;
        vm.accountData.menuIds = [];
        // api.selectRoleMenus(paras).then(function(data) {
        //   if (data.result == "操作成功") {
        //     menuIds = data.list;
        //     vm.getMenuIds(menuIds);
        //     vm.sysRoledata = data.list;
        //     vm.getList();
        //     setTimeout(function() {
        //       vm.$refs.tree.setCheckedKeys(vm.accountData.menuIds);
        //     }, 500);
        //   }
        // });
      }
      if (vm.currentRole.childRoleMode != null) {
        let defaultAreaList = [];
        if (
          vm.currentRole.childRoleMode != 1 &&
          vm.accountData.orgId.length > 0
        ) {
          defaultAreaList = vm.getDefaultAreaList();
        }
        vm.getAreaInfo(vm.currentRole.childRoleMode, defaultAreaList);
      }
    },
    init: function(data) {
      vm.show = true;
      setTimeout(function() {
        vm.open = true;
        vm.visible = true;
      }, 1);
      vm.roleType = 2;
      vm.isCanAddTag = user.roleMode <= 1;
      vm.hasSelectTagList = [];
      vm.selectTagList = [];
      console.log(data);
      if (data.userId != null) {
        vm.isAdd = false;
        vm.title = "编辑账号";
        vm.accountData = data;
        vm.accountData.menuIds = [];
        vm.accountData.campusId = []; //campusId

        vm.accountData.orgId = [];
        if (data.roleGroupId && data.roleGroupId != null) {
          vm.roleType = 1;
          vm.accountData.roleGroupId = data.roleGroupId;
        }
        if (data.userCampusList && data.userCampusList.length > 0) {
          for (let i = 0; i < data.userCampusList.length; i++) {
            let row = data.userCampusList[i];
            vm.accountData.orgId.push(row.orgId);
            vm.accountData.campusId.push([row.orgId, row.campusId]);
          }
           vm.api_getOtherData(data.userCampusList);
        }
        data.tagList.forEach(item => {
          vm.hasSelectTagList.push(item.tagId);
        });
        console.log(vm.hasSelectTagList);
        vm.orgChange();
      } else {
        vm.isAdd = true;
        vm.title = "新增账号";
        vm.accountData = {
          username: "",
          nickName: "",
          orgId: [],
          campusId: [],
          roleId: "",
          roleGroupId: "",
          menuIds: []
        };
         vm.api_getOtherData();
      }

      vm.api_getOrgSchoolList();
      vm.api_getOrgListByUser();
     
      vm.api_getRoleGroupData();
    },
    api_getRoleGroupData() {
      api.getRoleList().then(function(data) {
        if (data.resultCode == 2000) {
          vm.roleAttachList = data.list;
          if (vm.roleType == 1) {
            vm.currentRole = vm.roleAttachList.find(
              role => role.roleGroupId == vm.accountData.roleGroupId
            );
            vm.isHasSelectOrg = vm.currentRole.mainRoleMode <= 1;
            vm.isHasSelectCampus = vm.currentRole.mainRoleMode > 1;
            vm.isHasSelectArea = false;
            vm.accountData.menuIds = [];
            vm.sysRoledata = [];
            console.log(vm.currentRole);
            if (vm.currentRole.childRoleMode == null) {
              vm.isHasSelectArea = false;
            } else {
              vm.isHasSelectArea = true;
              console.log(vm.accountData.userPopedomList);
              vm.getAreaInfo(
                vm.currentRole.childRoleMode,
                vm.accountData.userPopedomList
              );
              console.log(vm.userPopedomList);
            }
          }
        } else {
          vm.$message.error(data.message);
        }
      });
    },
    api_getOtherData(idOfCampusAndOrg) {
      let paras = {};
      let param = { campusId: "",idOfCampusAndOrg:[]};
      if(idOfCampusAndOrg){
        param.idOfCampusAndOrg = idOfCampusAndOrg;
      }
      if (user.roleMode == 2) {
        param.campusId = vm.$store.state.campusId;
      }
      api
        .roleListForUser(param) //登录用户所能看到的角色列表
        .then(function(data) {
          if (data.resultCode == 2000) {
            vm.roleList = data.list;
            if (vm.roleType == 2) {
              if (vm.accountData.roleId == "") {
                vm.accountData.roleId = vm.roleList[0].roleId;
                vm.currentRole = vm.roleList[0];
                paras.roleId = vm.roleList[0].roleId;
                vm.isHasSelectOrg = vm.currentRole.roleMode <= 1;
                vm.isHasSelectCampus = vm.roleList[0].roleMode > 1;

                if (vm.roleList[0].childRoleMode == null) {
                  vm.isHasSelectArea = false;
                } else {
                  vm.isHasSelectArea = true;
                  vm.getAreaInfo(vm.roleList[0].childRoleMode, []);
                }
              } else {
                paras.roleId = vm.accountData.roleId;

                vm.currentRole = vm.roleList.find(
                  role => role.roleId == vm.accountData.roleId
                );
                vm.isHasSelectOrg = vm.currentRole.roleMode <= 1;
                vm.isHasSelectCampus = vm.currentRole.roleMode > 1;
                if (vm.currentRole.childRoleMode == null) {
                  vm.isHasSelectArea = false;
                } else {
                  vm.isHasSelectArea = true;
                  console.log(vm.accountData.userPopedomList);
                  vm.getAreaInfo(
                    vm.currentRole.childRoleMode,
                    vm.accountData.userPopedomList
                  );
                  console.log(vm.userPopedomList);
                }
              }
              // return api.selectRoleMenus(paras);
            }
            if (user.roleMode != 0) {
              vm.isAdmin = false;
            } else {
              vm.isAdmin = true;
            }
          } else {
            vm.$message.error(data.message);
          }
        });
      // .then(function(data) {
      //   if (data.resultCode == 2000) {
      //     let roleListTemp = data.list;

      //     vm.sysRoledata = data.list;
      //     //不是系统管理员
      //     if (user.roleMode != 0) {
      //       vm.isAdmin = false;
      //     } else {
      //       vm.isAdmin = true;
      //     }
      //     vm.getList();
      //     vm.getMenuIds(roleListTemp);
      //   } else {
      //     vm.$message.error(data.message);
      //   }
      // });
    },
    getAreaInfo(childRoleMode, areaList) {
      switch (childRoleMode) {
        case 1:
          vm.areaText = "";
          vm.isCanAddArea = false;
          vm.userPopedomList = [];
          vm.userPopedomList = [{ popedomCode: null, popedomName: "全部" }];
          break;
        case 2:
          vm.areaText = "添加省份";
          vm.isCanAddArea = true;
          vm.userPopedomList = [];
          vm.userPopedomList = areaList;
          break;
        case 3:
          vm.areaText = "添加城市";
          vm.isCanAddArea = true;
          vm.userPopedomList = [];
          vm.userPopedomList = areaList;
          break;
      }
    },

    cancel: function() {
      //隐藏弹窗
      vm.open = false;
      vm.isAdd = true;
      vm.selectTagList = [];
      vm.hasSelectTagList = [];
      setTimeout(function() {
        vm.show = false;
        vm.visible = false;
        vm.userPopedomList = [];
        vm.accountData = {};
        vm.currentRole = {};
        vm.isHasSelectOrg = true;
        vm.isHasSelectCampus = true;
        vm.isHasSelectArea = false;
        vm.isCanAddArea = false;
      }, 500);
    },
    sure: function() {
      if (vm.accountData.username == "") {
        this.$message.warning("登录账号不能为空");
        return;
      }
      if (!tool.judge("phone", vm.accountData.username)) {
        this.$message.warning("登录账号应为手机号");
        return;
      }
      if (vm.accountData.nickName == "") {
        this.$message.warning("账号名称不能为空");
        return;
      }
      if (!tool.judge("classname", vm.accountData.nickName)) {
        this.$message.warning("请输入有效的账号名称");
        return;
      }
      if (vm.isHasSelectOrg) {
        if (vm.accountData.orgId.length == 0) {
          this.$message.warning("所属机构不能为空");
          return;
        }
      }
      if (vm.isHasSelectArea) {
        if (!vm.userPopedomList || vm.userPopedomList.length == 0) {
          this.$message.warning("管辖范围不能为空");
          return;
        }
      }

      let campusIdList = [],
        orgId = null;
      if (vm.isHasSelectCampus) {
        if (vm.accountData.campusId.length == 0) {
          this.$message.warning("所属校区不能为空");
          return;
        }
        for (let i = 0; i < vm.accountData.campusId.length; i++) {
          let row = vm.accountData.campusId[i];
          campusIdList.push({
            orgId: row[0],
            campusId: row[1]
          });
        }
      } else {
        for (let i = 0; i < vm.accountData.orgId.length; i++) {
          let row = vm.accountData.orgId[i];
          campusIdList.push({
            orgId: row,
            campusId: null
          });
        }
      }
      orgId = campusIdList[0].orgId;
      // debugger
      // returns
      let tagList = [];
      for (let i = 0; i < vm.hasSelectTagList.length; i++) {
        tagList.push({ tagId: vm.hasSelectTagList[i] });
      }
      let paras = {
        username: vm.accountData.username,
        nickName: vm.accountData.nickName,
        account: vm.accountData.nickName,
        orgId: orgId + "",
        userCampusList: campusIdList,
        userPopedomList: [],
        tagList: tagList
      };
      if (vm.roleType == 1) {
        paras.roleGroupId = vm.accountData.roleGroupId;
      } else {
        paras.roleId = vm.accountData.roleId;
      }
      // if(vm.userPopedomList.length == 0){
      //   this.$message.warning("管辖范围不能为空");
      //     return;
      // }
      if (
        vm.userPopedomList.length == 1 &&
        vm.userPopedomList[0].popedomCode == null
      ) {
        paras.userPopedomList = [];
      } else {
        paras.userPopedomList = vm.userPopedomList;
      }

      if (vm.isAdd) {
        api.userAdd(paras).then(function(data) {
          console.log(data);
          if (data.resultCode == 2000) {
            vm.$message({
              message: "添加成功",
              type: "success"
            });
            vm.$emit("callback", "添加成功");
            vm.cancel();
          } else {
            vm.$message.error(data.message);
          }
        });
      } else {
        paras.userId = vm.accountData.userId;
        console.log(paras);
        api.userUpdate(paras).then(function(data) {
          console.log(data);
          if (data.resultCode == 2000) {
            vm.$message({
              message: "编辑成功",
              type: "success"
            });
            vm.$emit("callback", "添加成功");
            vm.cancel();
          } else {
            vm.$message.error(data.message);
          }
        });
      }
    },
    /**根据用户 获取机构校区级联表 */
    api_getOrgSchoolList() {
      vm.tagList = [];
      // debugger
      api.getOrgAndCampusLinkageListOfUser().then(function(data) {
        if (data.result == "操作成功") {
          vm.orgSchoolDict = [];
          for (let i = 0; i < data.list.length; i++) {
            let row = data.list[i];
            let campuses = row.campuses;
            // let children = [{value:"",label:"全部"}];
            let children = [];
            vm.orgAreaList.push({
              value: row.orgId,
              provinceRegions: row.provinceRegions,
              cityRegions: row.cityRegions
            });
            vm.tagList.push({
              orgId: row.orgId,
              tags: row.tags
            });
            for (let j = 0; j < campuses.length; j++) {
              children.push({
                value: campuses[j].campusId,
                label: campuses[j].name
              });
            }
            vm.orgSchoolDict.push({
              value: row.orgId,
              label: row.orgName,
              children: children
            });
          }
          vm.selectTagList = [];
          for (let i = 0; i < vm.accountData.orgId.length; i++) {
            for (let y = 0; y < vm.tagList.length; y++) {
              if (vm.accountData.orgId[i] == vm.tagList[y].orgId) {
                //去掉重复的标签
                var removalList = vm.tagList[y].tags.filter(newItem => {
                  return !vm.selectTagList.some(
                    defaultItem => defaultItem.tagId == newItem.tagId
                  );
                });
                Array.prototype.push.apply(vm.selectTagList, removalList);
              }
            }
          }
        }
      });
    },
    /**根据用户 获取机构列表 */
    api_getOrgListByUser() {
      // debugger
      api.getOrgListOfUser().then(function(data) {
        if (data.result == "操作成功") {
          vm.orgList = data.list;
        }
      });
    }
  },
  created: function() {
    vm = this;
    user = tool.getItem(lsKey.user, true); //得到当前用户信息
  }
};
</script>
<style lang="scss">
#accountAdd {
  .el-form-item__label {
    font-size: 0.16rem;
    color: #666666;
  }
  .el-checkbox__label {
    font-size: 0.16rem;
  }
  .el-radio.is-bordered + .el-radio.is-bordered {
    margin-left: 0rem;
  }
  .el-tag {
    font-size: 0.16rem;
  }
  .el-button--small {
    font-size: 0.16rem;
  }
  .el-cascader__tags .el-tag {
    max-width: 70%;
  }
  .el-cascader__search-input {
    min-width: 10%;
  }
}
</style>

<style scoped src="@/style/module/dialog.css"></style>
<style scoped>
.btn_div {
  padding: 0 0 0.3rem 0;
}
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}

.panel {
  width: 12rem;
}
.line_from {
  height: 1px;
  margin: 0rem 0.5rem 0.18rem 0.5rem;
  background-color: #f3f6fc;
}
.form {
  padding: 0 0.3rem 0.3rem 0;
  font-size: 0.16rem;
  color: #666666;
}
.ruleForm {
  padding-left: 0.5rem;
}
.el-radio-group {
  line-height: inherit;
}
.tree {
  padding: 0.1rem 0.5rem;
  /* max-height: 5.4rem; */
}
.el-col {
  border-radius: 4px;
}
.bg-purple {
  background: #d3dce6;
}
.grid-content {
  line-height: 0.5rem;
  text-align: center;
}
</style>
